package com.lovo.homework7_14ATM;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.InputMismatchException;

public class TransferPanel extends JPanel implements ActionListener {

    private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;

    private JLabel jLabel1;
    private JLabel jLabel2;

    private JTextField jTextField1;
    private JTextField jTextField2;

    private Icon buttonIcon1 = util.Util.getIcon("/images/buttonTansfer.png");
    private Icon buttonIcon2 = util.Util.getIcon("/images/buttonClear.jpg");
    private Icon buttonIcon3 = util.Util.getIcon("/images/buttonOff.jpg");

    private ATMmain atMmain;

    private boolean flagName = false;
    private int user = 0;

    public TransferPanel(ATMmain atMmain) {
        this.atMmain = atMmain;
        this.setLayout(null);

        this.setOpaque(false);

        jLabel1 = new JLabel("请输入转账的用户名");
        jLabel1.setBounds(300, 280, 250, 50);
        jLabel1.setForeground(Color.blue);
        jLabel1.setFont(new Font("宋体", Font.BOLD, 20));
        this.add(jLabel1);
        jTextField1 = new JTextField("");
        jTextField1.setBounds(500, 285, 200, 30);
        this.add(jTextField1);

        jLabel2 = new JLabel("请输入转账金额");
        jLabel2.setBounds(250, 385, 300, 50);
        jLabel2.setForeground(Color.blue);
        jLabel2.setFont(new Font("宋体", Font.BOLD, 20));
        this.add(jLabel2);
        jTextField2 = new JTextField("");
        jTextField2.setBounds(500, 390, 200, 30);
        this.add(jTextField2);

        jButton1 = new JButton(buttonIcon1);
        jButton1.setBounds(210, 450, 140, 50);
        this.add(jButton1);
        jButton1.addActionListener(this);

        jButton2 = new JButton(buttonIcon2);
        jButton2.setBounds(450, 450, 140, 50);
        this.add(jButton2);
        jButton2.addActionListener(this);

        jButton3 = new JButton(buttonIcon3);
        jButton3.setBounds(690, 450, 140, 50);
        this.add(jButton3);
        jButton3.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton1) {
            // 获取输入的对方账号
            String otherAccount = jTextField1.getText();
            double blance = 0;
            try {
                // 获取输入的转账金额
                blance = Double.parseDouble(jTextField2.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "转账金额必须为数字，并不能为空");
                return;
            }

            // 当期登录的账户
            Users loginUser = atMmain.getLoginUser();
            Users otherUser = null;
            ArrayList<Users> users = atMmain.getUserList();

            boolean isExist = false;
            for (int i = 0; i < users.size(); i++) {
                if (users.get(i).getUserName().equals(otherAccount)) {
                    isExist = true;
                    otherUser = users.get(i);
                    break;
                }
            }
            // 判断账号是否存在
            if (!isExist) {
                JOptionPane.showMessageDialog(this, "查无此人");
                return;
            }
            // 判断是否转给自己
            if (loginUser.getUserName().equals(otherAccount)) {
                JOptionPane.showMessageDialog(this, "不能转给自己");
                return;
            }
            // 判断余额是否足够
            if (blance > loginUser.getMoney()){
                JOptionPane.showMessageDialog(this, "余额不足");
                return;
            }
            // 当期账户扣钱
            loginUser.setMoney((int) (loginUser.getMoney() - blance));
            // 对方账户加钱
            otherUser.setMoney((int) (otherUser.getMoney() + blance));
            JOptionPane.showMessageDialog(this, "转账成功");

            //转文件
            util.FileChange.ObjectToBinary("111.dat",atMmain.getUserList());
            atMmain.switchPanel("menuPanel");
        } else if (e.getSource() == jButton2) {
            jTextField1.setText("");
            jTextField2.setText("");
            atMmain.switchPanel("transferPanel");
        } else if (e.getSource() == jButton3) {
            atMmain.switchPanel("loginPanel");
        }
    }
}
